#include "Battle.h"

Battle::Battle(Pokemon* pb,Pokemon* pf)
	:pokemon_Front(pf)
	,pokemon_Back(pb)
	,back_Offsetx(40)
	,back_Offsety(100)
	,front_Offsetx(205)
	,front_Offsety(50)
	,selectionIndex(0)
	,battleOver(false)
	,playerTurn(true)
{
	pokemon_Front->SetFrame(front_Offsetx,front_Offsety,pokemon_Front->Get1X(),pokemon_Front->Get2Y(),SPRITESIZE_X,SPRITESIZE_Y);
	pokemon_Back->SetFrame(back_Offsetx,back_Offsety,pokemon_Back->GetXBack(),pokemon_Back->GetYBack(),SPRITESIZE_X,SPRITESIZE_Y);

	front_HP = new SDLSprite("hpBar.png");
	back_HP = new SDLSprite("hpBar.png");
	
	pokemon_Front->SetVisible(true);
	pokemon_Back->SetVisible(true);
	front_HP->SetVisible(true);
	back_HP->SetVisible(true);
	front_HP->SetPosition(252,151);
	back_HP->SetPosition(61,57);

	StatsText(pokemon_Front, pokemon_Back);
	
	AttackBox(pokemon_Back);
	UpdateHealthBar();
}

void Battle::Start()
{
	line1 = new TypeWriter(10, "", "pokemon_pixel_font.ttf", 16, TextManager::SOLID);
	line1->SetPosition(10, 190);
	line1->SetTextColor(0, 0, 0);
	line2 = new TypeWriter(10, "", "pokemon_pixel_font.ttf", 16, TextManager::SOLID);
	line3 = new TypeWriter(10, "", "pokemon_pixel_font.ttf", 16, TextManager::SOLID);
	line3->SetPosition(10, 210);
	line3->SetTextColor(0, 0, 0);

	cursor = new SDLSprite("cursor.png");
	xRight = 250;
	xLeft = 190;
	yUp = 190;
	yDown = 212;

	x = xLeft;
	y = yUp;

	cursor->SetPosition(x, y);
	line1->SetNewContent("Que le combat commence!");
	line2->SetNewContent("");
}

void Battle::Update()
{
	StatsText(pokemon_Front, pokemon_Back);
	if(playerTurn)
	{
		if(SDLInput::GetInstance()->IsDown(SDLK_RIGHT))
		{
			x = xRight;
			cursor->SetPosition(x, y);
		}
		if(SDLInput::GetInstance()->IsDown(SDLK_LEFT))
		{
			x = xLeft;
			cursor->SetPosition(x, y);
		}
		if(SDLInput::GetInstance()->IsDown(SDLK_DOWN))
		{
			y = yDown;
			cursor->SetPosition(x, y);
		}
		if(SDLInput::GetInstance()->IsDown(SDLK_UP))
		{
			y = yUp;
			cursor->SetPosition(x, y);			
		}
		if(SDLInput::GetInstance()->IsDown(SDLK_RETURN))
		{
			if(x == xLeft && y == yUp)
			{
				selectionIndex = 1;
			}
	
			if(x == xLeft && y == yDown)
			{
				selectionIndex = 2;
			}
	
			if(x == xRight && y == yUp)
			{
				selectionIndex = 3;
			}
			if(x == xRight && y == yDown)
			{
				selectionIndex = 4;
			}

			Round();
			playerTurn = !playerTurn;
		}
	}
	else
	{
		Round();
		playerTurn = !playerTurn;
	}
}

void Battle::Stop()
{
	pokemon_Back->SetVisible(false);
	pokemon_Front->SetVisible(false);
}

void Battle::UpdateHealthBar()
{
	float fHP = pokemon_Front->GetHP();
	float bHP = pokemon_Back->GetHP();
	float fMHP = pokemon_Front->GetMaxHP();
	float bMHP = pokemon_Back->GetMaxHP();
	float fRatio = fHP / fMHP;
	float bRatio = bHP / bMHP;

	back_HP->SetFrame(252,151,0,0,bRatio * 59,2);
	front_HP->SetFrame(61,57,0,0,fRatio * 59,2);
}

void Battle::Round()
{
	if(playerTurn)
	{
		switch (selectionIndex)
		{
		case 1:
			pokemon_Back->DealDamage(pokemon_Back->GetAtk1(),pokemon_Front);
			line1->SetNewContent(pokemon_Back->GetName() + " utilise " + pokemon_Back->GetAtk1()->GetName());
			break;
		case 2:
			pokemon_Back->DealDamage(pokemon_Back->GetAtk2(),pokemon_Front);
			line1->SetNewContent(pokemon_Back->GetName() + " utilise " + pokemon_Back->GetAtk2()->GetName());
			break;
		case 3:
			pokemon_Back->DealDamage(pokemon_Back->GetAtk3(),pokemon_Front);
			line1->SetNewContent(pokemon_Back->GetName() + " utilise " + pokemon_Back->GetAtk3()->GetName());
			break;
		case 4:
			pokemon_Back->DealDamage(pokemon_Back->GetAtk4(),pokemon_Front);
			line1->SetNewContent(pokemon_Back->GetName() + " utilise " + pokemon_Back->GetAtk4()->GetName());
			break;

		default:
			pokemon_Back->DealDamage(pokemon_Back->GetAtk1(),pokemon_Front);
			break;
		}
	}
	else
	{
		int random_integer = (rand()%4) + 1;
		switch (random_integer)	
		{
		case 1:
			pokemon_Front->DealDamage(pokemon_Front->GetAtk1(),pokemon_Back);
			line3->SetNewContent(pokemon_Front->GetName() + " utilise " + pokemon_Front->GetAtk1()->GetName());
			break;
		case 2:
			pokemon_Front->DealDamage(pokemon_Front->GetAtk2(),pokemon_Back);
			line3->SetNewContent(pokemon_Front->GetName() + " utilise " + pokemon_Front->GetAtk2()->GetName());
			break;
		case 3:
			pokemon_Front->DealDamage(pokemon_Front->GetAtk3(),pokemon_Back);
			line3->SetNewContent(pokemon_Front->GetName() + " utilise " + pokemon_Front->GetAtk3()->GetName());
			break;
		case 4:
			pokemon_Front->DealDamage(pokemon_Front->GetAtk4(),pokemon_Back);
			line3->SetNewContent(pokemon_Front->GetName() + " utilise " + pokemon_Front->GetAtk4()->GetName());
			break;

		default:
			pokemon_Front->DealDamage(pokemon_Front->GetAtk1(),pokemon_Back);
			break;
		}
	}

	UpdateHealthBar();

	if(playerTurn)
		pokemon_Front->Tick();
	else
		pokemon_Back->Tick();

	if(pokemon_Back->OnDeath())
		OnPokemonDeath(true);
	else if (pokemon_Front->OnDeath())
		OnPokemonDeath(false);
}

void Battle::OnPokemonDeath(bool playersPokemon)
{
	if(playersPokemon)
	{
		line1->SetNewContent(" ");
		line2->SetNewContent(pokemon_Back->GetName() + "est mort");
		line3->SetNewContent("");
	}
	else
	{
		line1->SetNewContent(" ");
		line2->SetNewContent(pokemon_Front->GetName() + "est mort");
		line3->SetNewContent("");
	}

	battleOver = true;
}

void Battle::StatsText(Pokemon* pf,Pokemon* pb)
{
	SDLText* playerName = new SDLText(pb->GetName(), "pokemon_pixel_font.ttf");
	playerName->SetPosition(210, 130);
	playerName->SetTextSize(16);
	playerName->SetTextColor(255, 255, 255);

	SDLText* playerLevel = new SDLText("01", "pokemon_pixel_font.ttf");
	playerLevel->SetPosition(300, 130);
	playerLevel->SetTextSize(16);
	playerLevel->SetTextColor(255, 255, 255);

	SDLText* playerHp = new SDLText(to_string(pb->GetHP()), "pokemon_pixel_font.ttf");
	playerHp->SetPosition(250, 157);
	playerHp->SetTextSize(16);
	playerHp->SetTextColor(255, 255, 255);

	SDLText* playerMaxHp = new SDLText(to_string(pb->GetMaxHP()), "pokemon_pixel_font.ttf");
	playerMaxHp->SetPosition(290, 157);
	playerMaxHp->SetTextSize(14);
	playerMaxHp->SetTextColor(255, 255, 255);

	SDLText* computerName = new SDLText(pf->GetName(), "pokemon_pixel_font.ttf");
	computerName->SetPosition(30, 36);
	computerName->SetTextSize(16);
	computerName->SetTextColor(255, 255, 255);

	SDLText* computerLevel = new SDLText("01", "pokemon_pixel_font.ttf");
	computerLevel->SetPosition(110, 36);
	computerLevel->SetTextSize(16);
	computerLevel->SetTextColor(255, 255, 255);
}

void Battle::DynamicText(string s)
{
	vector<string> vString = StringUtils::Explode(" ", s);
	stringstream s1, s2;

	int letterCount = 0; 
	for( vector<string>::iterator iter = vString.begin(); iter < vString.end(); ++iter)
	{
		if(letterCount <= 32)
		{
			string str = *iter;
			s1 << str << " ";
			letterCount += str.length() + 1;
		}
		else
		{
			string str = *iter;
			s2 << str << " ";
		}
	}

	if(!line1->ContentEquals(s1.str()))
	{
		line1->SetNewContent(s1.str());
		line1->SetPosition(10, 190);
		line1->SetTextColor(0, 0, 0);
	}

	if(line1->IsCompleted() && !line2->ContentEquals(s2.str()))
	{
		line2->SetNewContent(s2.str());
		line2->SetPosition(10, 210);
		line2->SetTextColor(0, 0, 0);		
	}

}

void Battle::AttackBox(Pokemon* pb)
{
	attackBox = new SDLSprite("AttackBox.png");
	cursor = new SDLSprite("cursor.png");
	attackText1 = new SDLText(pb->GetAtk1()->GetName(), "pokemon_pixel_font.ttf", 16, TextManager::SOLID);
	attackText1->SetPosition(200, 188);
	attackText1->SetTextColor(0, 0, 0);

	attackText2 = new SDLText(pb->GetAtk2()->GetName(), "pokemon_pixel_font.ttf", 16, TextManager::SOLID);
	attackText2->SetPosition(200, 210);
	attackText2->SetTextColor(0, 0, 0);

	attackText3 = new SDLText(pb->GetAtk3()->GetName(), "pokemon_pixel_font.ttf", 16, TextManager::SOLID);
	attackText3->SetPosition(260, 188);
	attackText3->SetTextColor(0, 0, 0);

	attackText4 = new SDLText(pb->GetAtk4()->GetName(), "pokemon_pixel_font.ttf", 16, TextManager::SOLID);
	attackText4->SetPosition(260, 210);
	attackText4->SetTextColor(0, 0, 0);
}
